package com.zjk.storage.core.progress;

import cn.hutool.core.io.IoUtil;
import com.zjk.storage.core.aspect.FileStorageAspect;
import com.zjk.storage.core.aspect.download.DownloadAspectChain;
import com.zjk.storage.core.aspect.download.DownloadThumbAspectChain;
import com.zjk.storage.core.entity.BaseFileInfo;
import com.zjk.storage.core.provider.FileStorage;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class Downloader {
    public static final int TARGET_FILE = 1;
    public static final int TARGET_THUNB_FILE = 2;

    private final FileStorage fileStorage;
    private final List<FileStorageAspect> aspectList;
    private final BaseFileInfo fileInfo;
    private final Integer target;
    private ProgressListener progressListener;

    public Downloader(FileStorage fileStorage, List<FileStorageAspect> aspectList, BaseFileInfo fileInfo, Integer target) {
        this.fileStorage = fileStorage;
        this.aspectList = aspectList;
        this.fileInfo = fileInfo;
        this.target = target;
    }


    public Downloader setProgressMonitor(ProgressListener progressListener) {
        this.progressListener = progressListener;
        return this;
    }

    public Downloader setProgressMonitor(Consumer<Long> progressListener) {
        return setProgressMonitor((progressSize,allSize) -> progressListener.accept(progressSize));
    }

    public Downloader setProgressMonitor(BiConsumer<Long,Long> progressListener) {
        return setProgressMonitor(new ProgressListener() {
            @Override
            public void start() {
                System.out.println("开始下载");
            }

            @Override
            public void progress(long progressSize,long allSize) {
                progressListener.accept(progressSize,allSize);
            }

            @Override
            public void finish() {
                System.out.println("下载完成");
            }
        });
    }

    public void inputStream(Consumer<InputStream> consumer) {
        switch (target) {
            case TARGET_FILE:
                new DownloadAspectChain(aspectList, (_fileInfo, _fileStorage, _consumer) -> _fileStorage.download(_fileInfo, _consumer))
                        .next(fileInfo, fileStorage,inputStream -> consumer.accept(progressListener == null?inputStream:new ProgressInputStream(inputStream,progressListener,fileInfo.getSize())));
                break;
            case TARGET_THUNB_FILE:
                new DownloadThumbAspectChain().
                        next(fileInfo, fileStorage,inputStream -> consumer.accept(progressListener == null?inputStream:new ProgressInputStream(inputStream,progressListener,fileInfo.getThumbSize())));
                break;
            default:
                throw new IllegalArgumentException("不支持的下载目标类型：" + target);
        }
    }

    public void outputStream(OutputStream out) {
        inputStream(in -> IoUtil.copy(in,out));
    }
}
